package com.pethospital.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pethospital.common.Result;
import com.pethospital.dto.MedicalRecordDTO;
import com.pethospital.entity.MedicalRecord;
import com.pethospital.service.MedicalRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * 健康档案控制器
 */
@RestController
@RequestMapping("/medical")
@Slf4j
public class MedicalRecordController {

    @Resource
    private MedicalRecordService medicalRecordService;

    /**
     * 添加健康档案
     */
    @PostMapping
    public Result<Long> addMedicalRecord(@RequestBody @Valid MedicalRecordDTO medicalRecordDTO, HttpServletRequest request) {
        Long staffId = (Long) request.getAttribute("userId");
        log.info("添加健康档案, 医生ID: {}, 数据: {}", staffId, medicalRecordDTO);
        
        Long recordId = medicalRecordService.addMedicalRecord(medicalRecordDTO, staffId);
        return Result.success(recordId);
    }

    /**
     * 获取健康档案详情
     */
    @GetMapping("/{id}")
    public Result<MedicalRecord> getMedicalRecord(@PathVariable Long id) {
        log.info("获取健康档案详情, ID: {}", id);
        
        MedicalRecord medicalRecord = medicalRecordService.getMedicalRecord(id);
        if (medicalRecord == null) {
            return Result.error("健康档案不存在");
        }
        
        return Result.success(medicalRecord);
    }

    /**
     * 更新健康档案
     */
    @PutMapping("/{id}")
    public Result<Boolean> updateMedicalRecord(@PathVariable Long id, @RequestBody @Valid MedicalRecordDTO medicalRecordDTO, HttpServletRequest request) {
        Long staffId = (Long) request.getAttribute("userId");
        log.info("更新健康档案, ID: {}, 医生ID: {}, 数据: {}", id, staffId, medicalRecordDTO);
        
        boolean result = medicalRecordService.updateMedicalRecord(id, medicalRecordDTO, staffId);
        return Result.success(result);
    }

    /**
     * 获取宠物健康档案列表
     */
    @GetMapping("/pet/{petId}")
    public Result<List<MedicalRecord>> getPetMedicalRecords(@PathVariable Long petId) {
        log.info("获取宠物健康档案列表, 宠物ID: {}", petId);
        
        List<MedicalRecord> records = medicalRecordService.getPetMedicalRecords(petId);
        return Result.success(records);
    }

    /**
     * 获取宠物健康档案分页
     */
    @GetMapping("/pet/{petId}/page")
    public Result<Page<MedicalRecord>> getPetMedicalRecordsPage(
            @PathVariable Long petId,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {
        log.info("获取宠物健康档案分页, 宠物ID: {}, 页码: {}, 每页大小: {}", petId, page, size);
        
        Page<MedicalRecord> recordPage = medicalRecordService.getPetMedicalRecordsPage(petId, page, size);
        return Result.success(recordPage);
    }

    /**
     * 获取医生创建的健康档案
     */
    @GetMapping("/staff")
    public Result<Page<MedicalRecord>> getStaffMedicalRecords(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            HttpServletRequest request) {
        Long staffId = (Long) request.getAttribute("userId");
        log.info("获取医生创建的健康档案, 医生ID: {}, 页码: {}, 每页大小: {}", staffId, page, size);
        
        Page<MedicalRecord> recordPage = medicalRecordService.getStaffMedicalRecords(staffId, page, size);
        return Result.success(recordPage);
    }

    /**
     * 根据预约ID获取健康档案
     */
    @GetMapping("/appointment/{appointmentId}")
    public Result<MedicalRecord> getMedicalRecordByAppointmentId(@PathVariable Long appointmentId) {
        log.info("根据预约ID获取健康档案, 预约ID: {}", appointmentId);
        
        MedicalRecord medicalRecord = medicalRecordService.getMedicalRecordByAppointmentId(appointmentId);
        return Result.success(medicalRecord);
    }
} 